import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.globals import ThemeType
import akshare as ak

# A股股票数据 - 实时行情数据 - 东财
# df = ak.stock_zh_index_spot_em(symbol='中证系列指数')
# print(df)

# A股股票数据 - 实时行情数据 - 新浪
# df = ak.stock_zh_index_spot_sina()
# print(df)

# 指数估值
symbol = "沪深300"
df = ak.index_value_hist_funddb(symbol=symbol, indicator="市盈率")
df2 = ak.index_value_hist_funddb(symbol=symbol, indicator="市净率")

chart = Line(
    # 设置主题、画布宽度、高度等信息
    init_opts=opts.InitOpts(theme=ThemeType.DARK, width="1200px", height="600px"),
    # 是否内嵌js 默认false
    # render_opts=opts.RenderOpts(is_embed_js=True)
)

chart.set_global_opts(
    title_opts=opts.TitleOpts(title="中证环保"),
    # 工具箱配置
    toolbox_opts=opts.ToolboxOpts(is_show=True),
    # 区域缩放配置
    datazoom_opts=[opts.DataZoomOpts(is_show=True)],
)

x_data = df["日期"].tolist()
y_data = df["市盈率"].tolist()
y_data2 = df2["市净率"].tolist()

chart.add_xaxis(x_data)
chart.add_yaxis(
"市盈率",
            y_data,
            markpoint_opts=opts.MarkPointOpts(
                data=[opts.MarkPointItem(type_="max"), opts.MarkPointItem(type_="min")],
            ),
            # 标记线
            markline_opts=opts.MarkLineOpts(
                data=[opts.MarkLineItem(type_="average")]
            ),
)
chart.add_yaxis("市净率", y_data2, is_smooth=True)

chart.render(f"{symbol}.html")